<?php


namespace App\Controller\Admin;


use App\Annotation\OperationLog;
use App\Controller\AbstractController;
use App\Request\Admin\AdminRoleSaveRequest;
use App\Service\Admin\AdminRoleService;
use Hyperf\Di\Annotation\Inject;

class AdminRoleController extends AbstractController
{
    /**
     * @Inject()
     * @var AdminRoleService
     */
    protected $adminRoleService;

    /**
     * 角色列表
     * @OperationLog(event="角色列表",platform="admin")
     */
    public function index()
    {
        $where = $this->request->query();
        $list = $this->adminRoleService->index($where);
        return $this->response->success($list);
    }

    /**
     * 所有角色
     * @OperationLog(event="获取所有角色",platform="admin")
     */
    public function all(){
        $list = $this->adminRoleService->all([]);
        return $this->response->success($list);
    }

    /**
     * @OperationLog(event="添加角色",platform="admin")
     * @param $id
     * @param AdminRoleSaveRequest $request
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function save($id, AdminRoleSaveRequest $request)
    {
        $validated = $request->validated();

        $role = $this->adminRoleService->save($id, $validated);

        return $this->response->success($role);
    }

    /**
     * @OperationLog(event="删除角色",platform="admin")
     * @param int $id
     * @return mixed|\Psr\Http\Message\ResponseInterface
     */
    public function delete(int $id)
    {
        $this->adminRoleService->delete($id);
        return $this->response->success();
    }

    /**
     * 获取角色对应的菜单
     * @OperationLog(event="获取角色菜单",platform="admin")
     * @param int $id
     * @return array
     */
    public function menus(int $id)
    {
        $menus = $this->adminRoleService->adminMenus($id);
        return $this->response->success($menus);
    }
}